package ltd.hxya.novel.springsecurity.expression;

import ltd.hxya.novel.springsecurity.entity.UserDetail;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("expression")
public class NovelExpression  {

    public Boolean hasAuthority(String permission){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserDetail userDetail = (UserDetail) authentication.getPrincipal();
        List<String> permissions = userDetail.getPermissions();
        if (permissions.contains("*")){
            return true;
        }
        return permissions.contains(permission);
    }
}
